package com.wg.msuser.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wg.msuser.entity.User;
import com.wg.msuser.entity.dto.QueryPageDto;
import com.wg.msuser.service.UserService;
import com.wg.msuser.util.StringUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "UserController", description = "用户控制器")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Operation(summary = "用户注册")
    @PostMapping("/user")
    @Parameter(name = "user", description = "用户信息对象")
    public int doRegister(@RequestBody User user) {
        System.out.println( user);

        return userService.registerUser(user);
    }

    @Operation(summary = "用户登录")
    @GetMapping("/user")
    public Map<String,Object> doLogin(@Parameter(description = "用户信息对象") User user){
        System.out.println(user);
        User user1 = userService.loginUser(user);
        System.out.println(user1);
        Map<String, Object>result=  new HashMap<>();
        if (user1 != null) {
            // 登录成功，生成token
            StpUtil.login(user1.getId()); // 使用用户ID作为登录标识
            String token = StpUtil.getTokenValue();

            result.put("code", 200);
            result.put("msg", "登录成功");
            result.put("data", user1);
            result.put("token", token);
        } else {
            result.put("code", 401);
            result.put("msg", "用户名或密码错误");
        }

        return result;
    }

    @Operation(summary = "更新用户信息")
    @PutMapping("/user")
    @Parameter(name = "user", description = "用户信息对象")
    public int doUpdate(@RequestBody User user){
        return userService.updateUser(user)?1:0;
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/user/{userId}")
    @Parameter(name = "userId", description = "用户编号")
    public int doDelete(@PathVariable("userId") String id){
        return userService.deleteUser(Integer.parseInt(id))?1:0;
    }

    @Operation(summary = "查询用户")
    @GetMapping("/user/list")
    @Parameters({
            @Parameter(name = "user",description = "用户信息对象"),
            @Parameter(name = "pager",description = "分页信息对象")
    })
    public Page<User> doList(User user, QueryPageDto pager){
        return userService.selectPage(user,pager);
    }


    @Operation(summary = "通过用户名查询用户")
    @GetMapping("/user/getUserByUserName/{userName}")
    public User getUserByUserName(@PathVariable("userName") String userName){
        return userService.getUserByUserName(userName);
    }

    @Operation(summary = "通过邮箱查询用户")
    @GetMapping("/user/getUserByEmail/{email}")
    public User getUserByEmail(@PathVariable("email") String email){
        return userService.getUserByEmail(email);
    }

    @Operation(summary = "通过ID查询用户")
    @GetMapping("/user/getUserById/{id}")
    public User getUserById(@PathVariable("id") String id){
        return userService.getById(id);
    }
}
